#include "ConnectionPool.h"
#include <thread>
#include <iostream>
using namespace std;

#if 1
void task(ConnectionPool* pool)
{
    string sql = "INSERT INTO test_users (username, email, age) VALUES ('zhangsan', 'user@example.com', 25)";

    auto conn = pool->getConnection();
    conn->update(sql);
    std::cout << "连接: " << (void*)conn.get() << ", 执行SQL完成\n";

    std::this_thread::sleep_for(std::chrono::seconds(10));
}

int main()
{
    ConnectionPool* pool = ConnectionPool::getConnectionPool();

    for (int i = 0; i < 20; ++i)
    {
        thread t(task, pool);
        t.detach();
    }

    getchar();
    return 0;
}

#else

int main()
{
    ConnectionPool* pool = nullptr;
    try 
    {
        pool = ConnectionPool::getConnectionPool();
    }
    catch (const std::exception& e)
    {
        cerr << "error: " << e.what() << endl;
    }

    string sql = "INSERT INTO test_users (username, email, age) VALUES ('zhangsan', 'user@example.com', 25)";

    auto conn = pool->getConnection();
    conn->update(sql);

    return 0;
}
#endif